package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.bean.Square;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.FollowSquareListPresenter;
import com.example.peibei.service.presenter.FollowSquarePresenter;
import com.example.peibei.service.presenter.SquareListPresenter;
import com.example.peibei.ui.adapter.MyBillListAdapter;
import com.example.peibei.ui.adapter.SquareListAdapter;
import com.example.peibei.ui.fragment.QuanZiFragment;
import com.example.peibei.ui.fragment.SquareListFragment;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SquareFollowListActivity extends WDActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    private SPUtils spUtils;
    private String token;
    private List<Square.RecordsDTO> followList = new ArrayList<>();
    private SquareListAdapter squareListAdapter;
    private FollowSquarePresenter followSquarePresenter;
    private FollowSquareListPresenter giveUpPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_i_send_bill;
    }

    @Override
    protected void initView() {
        tv_title.setText("我关注的圈子");
        spUtils = new SPUtils(SquareFollowListActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        order_list.setLayoutManager(new LinearLayoutManager(this));
        squareListAdapter = new SquareListAdapter(followList);
        order_list.setAdapter(squareListAdapter);
        order_list.setEmptyView(empty_view);
        followSquarePresenter = new FollowSquarePresenter(new FollowSquareCall());
        giveUpPresenter = new FollowSquareListPresenter(new FollowCall());
        squareListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<Square.RecordsDTO> data = (List<Square.RecordsDTO>) adapter.getData();
                Square.RecordsDTO recordsDTO = data.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("imgUrl", recordsDTO.getHeaderUrl());
                bundle.putString("backUrl", recordsDTO.getBackUrl());
                bundle.putString("circleName", recordsDTO.getCircleName());
                bundle.putString("circleTypeId", recordsDTO.getCircleTypeId());
                bundle.putString("id", recordsDTO.getId() + "");
                bundle.putString("personNum", recordsDTO.getPersonNum());
                bundle.putBoolean("follow", recordsDTO.isFollow());
                intent(SquareDetailActivity.class, bundle);
            }
        });
        squareListAdapter.addChildClickViewIds(R.id.rl_follows);
        squareListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.rl_follows) {
                    if (!TextUtils.isEmpty(token)) {
                        List<Square.RecordsDTO> data = (List<Square.RecordsDTO>) adapter.getData();
                        Square.RecordsDTO recordsDTO = data.get(position);
                        boolean follow = recordsDTO.isFollow();
                        giveUpPresenter.reqeust(follow, "Bearer " + token, recordsDTO.getId() + "");
                    } else {
                        intent(LoginActivity.class);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        followSquarePresenter.reqeust(true, "Bearer " + token);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    class FollowSquareCall implements DataCall<Square> {
        @Override
        public void success(Square data, Object... args) {
            //添加列表并刷新
            if (followSquarePresenter.getPage() == 1) {
                followList.clear();
            }
            List<Square.RecordsDTO> records = data.getRecords();
            if (records != null && records.size() > 0) {
                for (int i = 0; i < records.size(); i++) {
                    records.get(i).setFollow(true);
                }
                squareListAdapter.setNewInstance(records);
            }else{
                squareListAdapter.setNewInstance(data.getRecords());
            }
            squareListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class FollowCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            followSquarePresenter.reqeust(true, "Bearer " + token);
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    @Override
    protected void destoryData() {

    }
}
